package org.x.note.swing;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.x.note.file.FileUtil;
import org.x.note.file.ZipUtil;

@SuppressWarnings("serial")
public class FileUpload extends JFrame {

	private JTextField textField_2;
	private JTextField textField;

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileUpload frame = new FileUpload();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame
	 */
	public FileUpload() {
		super();
		setTitle("操作文件");
		setBounds(100, 100, 433, 184);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		textField = new JTextField();
		textField.setBounds(10, 10, 300, 20);
		getContentPane().add(textField);

		final JButton button = new JButton();
		button.addActionListener(new ActionListener() {
			@SuppressWarnings("static-access")
			public void actionPerformed(final ActionEvent arg0) {
				JFileChooser jFile = new JFileChooser();
				jFile.setMultiSelectionEnabled(true);// 设置文件多选
				jFile.setFileSelectionMode(jFile.FILES_AND_DIRECTORIES);// 设置文件或文件夹都可选
				jFile.showDialog(jFile, "请选择");
				File[] source = jFile.getSelectedFiles();
				if (source != null) {
					StringBuffer sb = new StringBuffer();
					String strs = null;
					for (int i = 0; i < source.length; i++) {
						strs = source[i].getAbsolutePath() + ";";
						if (i == source.length - 1) {
							strs = source[i].getAbsolutePath();
						}
						sb.append(strs);
					}
					textField.setText(sb.toString());
				}

			}
		});
		button.setBounds(320, 10, 80, 20);
		button.setText("源文件");
		getContentPane().add(button);

		textField_2 = new JTextField();
		textField_2.setBounds(10, 40, 300, 20);
		getContentPane().add(textField_2);

		JButton button_1 = new JButton();
		button_1.addActionListener(new ActionListener() {
			@SuppressWarnings("static-access")
			public void actionPerformed(final ActionEvent arg0) {
				JFileChooser jFile = new JFileChooser();
				jFile.setFileSelectionMode(jFile.FILES_AND_DIRECTORIES);// 设置文件或文件夹都可选
				jFile.showDialog(jFile, "请选择");
				File target = jFile.getSelectedFile();
				if (target != null) {
					textField_2.setText(target.getAbsolutePath());
				}
			}
		});
		button_1.setBounds(320, 40, 80, 20);
		button_1.setText("目标文件");
		getContentPane().add(button_1);

		JButton button_2 = new JButton();
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				String tar = textField_2.getText();
				String sou = textField.getText();
				if (sou == null || "".equals(sou)) {
					JOptionPane.showMessageDialog(null, "请选择要复制的文件！");
				} else if (tar == null || "".equals(tar)) {
					JOptionPane.showMessageDialog(null, "请选择目标的文件！");
				} else {
					try{
						if (sou.indexOf(";") != -1) {
							String[] sources = sou.split(";");
							for (int i = 0; i < sources.length; i++) {
								FileUtil.copyFile(sources[i], tar);
							}
						}
						FileUtil.copyFile(sou, tar);
					}catch (IOException ioe) {
						JOptionPane.showMessageDialog(null, "复制失败！");
					}
					
					JOptionPane.showMessageDialog(null, "复制成功！");
				}
			}
		});
		button_2.setText("复制");
		button_2.setBounds(10, 70, 60, 20);
		getContentPane().add(button_2);

		JButton button_4 = new JButton();
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				String tar = textField_2.getText();
				String sou = textField.getText();
				if (sou == null || "".equals(sou)) {
					JOptionPane.showMessageDialog(null, "请选择要解压的文件！");
				} else if (tar == null || "".equals(tar)) {
					JOptionPane.showMessageDialog(null, "请选择目标的文件！");
				} else {
					try {
						FileUtil.unZipFile(sou, tar);
					} catch (IOException ioe) {
						JOptionPane.showMessageDialog(null, "解压失败！");
					}
					JOptionPane.showMessageDialog(null, "解压成功！");
				}

			}
		});
		button_4.setBounds(190, 70, 60, 20);
		button_4.setText("解压");
		getContentPane().add(button_4);

		JButton button_3 = new JButton();
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				String tar = textField_2.getText();
				String sou = textField.getText();

				if (sou == null || "".equals(sou)) {
					JOptionPane.showMessageDialog(null, "请选择要压缩的文件！");
				} else if (tar == null || "".equals(tar)) {
					JOptionPane.showMessageDialog(null, "请选择目标的文件！");
				} else {
					try {
						ZipUtil.compress(sou, tar);
					} catch (IOException ioe) {
						JOptionPane.showMessageDialog(null, "压缩失败！");
					}
					JOptionPane.showMessageDialog(null, "压缩成功！");
				}
			}
		});
		button_3.setText("压缩");
		button_3.setBounds(100, 70, 60, 20);
		getContentPane().add(button_3);

		JButton button_5 = new JButton();
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				String sou = textField.getText();
				if (sou == null || "".equals(sou)) {
					JOptionPane.showMessageDialog(null, "请选择要删除的文件或文件夹！");
				} else {
					int a = JOptionPane.showConfirmDialog(null, "确定要删除吗？");
					if (a == 0) {
						if (sou.indexOf(";") != -1) {
							String[] strs = sou.split(";");
							try{
								for (int i = 0; i < strs.length; i++) {
									FileUtil.delFile(strs[i]);
								}
							}catch (IOException ioe) {
								JOptionPane.showMessageDialog(null, "删除失败！");
							}
							
							JOptionPane.showMessageDialog(null, "删除成功！");
							textField.setText("");
						} else {
							try{
								FileUtil.delFile(sou);
							}catch (IOException ioe) {
								JOptionPane.showMessageDialog(null, "删除失败！");
							}
							JOptionPane.showMessageDialog(null, "删除成功！");
							textField.setText("");
						}
					}
				}

			}
		});
		button_5.setBounds(280, 70, 60, 20);
		button_5.setText("删除");
		getContentPane().add(button_5);

		JButton button_6 = new JButton();
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				String tar = textField_2.getText();
				String sou = textField.getText();
				if (sou == null || "".equals(sou)) {
					JOptionPane.showMessageDialog(null, "请选择要剪切的文件或文件夹！");
				} else if (tar == null || "".equals(tar)) {
					JOptionPane.showMessageDialog(null, "请选择剪切的文件或文件夹存放路径！");
				} else {
					int a = JOptionPane.showConfirmDialog(null, "确定要剪切吗？");
					if (a == 0) {
						try {
							FileUtil.cutFile(sou, tar);
						} catch (IOException ioe) {
							JOptionPane.showMessageDialog(null, "剪切失败！");
						}
						JOptionPane.showMessageDialog(null, "剪切成功！");
						textField.setText("");
					}
				}
			}
		});
		button_6.setBounds(360, 70, 60, 20);
		button_6.setText("剪切");
		getContentPane().add(button_6);
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 40, 180, 400);
		getContentPane().add(scrollPane);
		//
	}
}

